package com.crayfish.quduo.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.PersonalPresenter;
import com.crayfish.quduo.presenter.contract.PersonalContract;
import com.crayfish.quduo.ui.fragment.EditFragmentDialog;
import com.crayfish.quduo.utils.ImageUtil;
import com.crayfish.quduo.utils.SystemUtil;
import com.crayfish.quduo.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人中心
 * Created by crayfish on 2016/11/24.
 */
public class PersonalActivity extends BaseMvpActivity<PersonalContract.Presenter>
        implements PersonalContract.View{

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.txt_introduction)
    TextView txt_introduction;
    @BindView(R.id.txt_user)
    TextView txt_user;
    @BindView(R.id.txt_phone)
    TextView txt_phone;
    @BindView(R.id.txt_email)
    TextView txt_email;
    @BindView(R.id.txt_gender)
    TextView txt_gender;
    @BindView(R.id.iv_portrait)
    ImageView iv_portrait;

    @Override
    protected PersonalContract.Presenter createPresenter() {
        return new PersonalPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my;
    }

    @Override
    protected void initEventAndData() {
        mPresenter.getUser();
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @OnClick(R.id.txt_introduction)
    void introductionClick(){
        EditFragmentDialog dialog = EditFragmentDialog.newInstance(txt_introduction.getText().toString());
        dialog.show(getSupportFragmentManager(),"简介");
        dialog.setOnEditContentListener(new EditFragmentDialog.EditContentListener() {
            @Override
            public void onEditContent(String Str) {
                mPresenter.updateIntroduction(Str);
            }
        });
    }

    @OnClick(R.id.txt_user)
    void userClick(){
        EditFragmentDialog dialog = EditFragmentDialog.newInstance(txt_user.getText().toString());
        dialog.show(getSupportFragmentManager(),"用户名");
        dialog.setOnEditContentListener(new EditFragmentDialog.EditContentListener() {
            @Override
            public void onEditContent(String Str) {
                mPresenter.updateUsername(Str);
            }
        });
    }
    @OnClick(R.id.txt_phone)
    void phoneClick(){
        EditFragmentDialog dialog = EditFragmentDialog.newInstance(txt_phone.getText().toString());
        dialog.show(getSupportFragmentManager(),"电话");
        dialog.setOnEditContentListener(new EditFragmentDialog.EditContentListener() {
            @Override
            public void onEditContent(String Str) {
                mPresenter.updatePhone(Str);
            }
        });
    }
    @OnClick(R.id.txt_email)
    void emailClick(){
        EditFragmentDialog dialog = EditFragmentDialog.newInstance(txt_email.getText().toString());
        dialog.show(getSupportFragmentManager(),"邮箱");
        dialog.setOnEditContentListener(new EditFragmentDialog.EditContentListener() {
            @Override
            public void onEditContent(String Str) {
                mPresenter.updateEmail(Str);
            }
        });
    }
    @OnClick(R.id.txt_gender)
    void genderClick(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        final String[] items = new String[]{"男", "女"};
        builder.setTitle("性别")
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mPresenter.updateGender(items[which]);
                    }
                })
                .create();
        builder.show();
    }

    @Override
    public void showUser(UserBean userBean) {
        if(!SystemUtil.isEmpty(userBean.getIntroduction())) {
            txt_introduction.setText(userBean.getIntroduction());
        }
        txt_user.setText(userBean.getUsername());
        txt_email.setText(userBean.getEmail());
        txt_gender.setText(userBean.getGender());
        txt_phone.setText(userBean.getMobilePhoneNumber());
        ImageUtil.imageBitmapLoad(this,userBean.getPortrait(),iv_portrait);
    }

    @Override
    public void showError(String msg) {
        ToastUtil.show(msg);
    }
}
